import datetime
import json
import os
import pprint
import random
import time
import requests
import hashlib


def get_nonce():
    """
        生成一个指定长度的随机字符串
        """
    randomlength = 16
    random_str = ''
    base_str = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
    length = len(base_str) - 1
    for i in range(randomlength):
        random_str += base_str[random.randint(0, length)]
    # print(random_str)
    return random_str


def signIn(ck, msg=None):
    url = 'https://zhiwen.xfyun.cn/api/user/signIn'
    headers = {
        'Cookie': ck,
        'Content-Type': 'application/json'
    }
    response = requests.post(url=url, headers=headers).json()
    msg += ('签到返回: ' + json.dumps(response))
    return msg


def webhook(message, webhookToken):
    url = f'http://www.pushplus.plus/send'
    headers = {
        'Content-Type': 'application/json'
    }
    data = {
        "token": webhookToken,
        "title": "讯飞智文",
        "content": message,
        "template": "json"
    }
    response = requests.post(url=url, data=json.dumps(data), headers=headers).json()
    print(f"企业微信推送成功{response}")

def sleepmoment():
    random_seconds = random.randint(1, 10)
    time.sleep(random_seconds)

if __name__ == '__main__':
    sleepmoment()
    cks = os.environ['xfzw']
    cks = cks.split('@')
    webhookToken = os.environ['PUSH_PLUS_TOKEN']
    msg = ""
    for ck in cks:
        msg += signIn(ck, msg)

    print(f'消息:  {msg}')
    webhook(msg, webhookToken)
